package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.pojo.MaxAndMinLatAndLng;
import com.wn.common.pojo.PageParam;
import com.wn.common.pojo.Rsp;
import com.wn.common.pojo.ScenicSpots;
import com.wn.mapper.ScenicSpotsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
public class ScenicSpotsService {
    @Autowired
    private ScenicSpotsMapper scenicSpotsMapper;

    //查询所有景点
    public PageInfo<ScenicSpots> queryAll(PageParam<ScenicSpots> pageParam){
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //分页
            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
            List<ScenicSpots> scenicSpotslList = scenicSpotsMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<ScenicSpots> scenicSpotsPageInfo = new PageInfo<ScenicSpots>(scenicSpotslList);
            return scenicSpotsPageInfo;
        }else {
            PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
            List<ScenicSpots> scenicSpots = scenicSpotsMapper.queryAll();
           //将数组保存在pageInfo中
            PageInfo<ScenicSpots> scenicSpotsPageInfo = new PageInfo<ScenicSpots>(scenicSpots);
            return scenicSpotsPageInfo;
        }
    }

    //根据id查询
    public Rsp getScenicSpotsById(Integer id){
        ScenicSpots scenicSpots = scenicSpotsMapper.selectByPrimaryKey(id);
        if(StringUtils.isEmpty(scenicSpots)){
            return new Rsp().msg("查询失败");
        }else{
            return new Rsp().success(scenicSpots);
        }
    }

    //添加景点
    public Rsp insertAll(ScenicSpots scenicSpots){
        int i = scenicSpotsMapper.insertSelective(scenicSpots);
        System.out.println(i);
        if(i == 1){
            return new Rsp().success(i);
        }else {
            return new Rsp().msg("添加失败！");
        }
    }
    //更新景点
    public  Rsp UpdateScenicSpots(ScenicSpots scenicSpots){
        int i = scenicSpotsMapper.updateByPrimaryKeySelective(scenicSpots);
        if(i == 1){
            return new Rsp().success(i);
        }else{
            return new Rsp().msg("更新失败！");
        }
    }
    //根据id删除景点
    public Rsp deleteByPrimaryKey(ScenicSpots scenicSpots){
        int i = scenicSpotsMapper.deleteByPrimaryKey(scenicSpots.getId());
        if(i != 0){
            return new Rsp().success(i);
        }else{
            return new Rsp().msg("删除失败！");
        }
    }

    /**
     * 修改状态
     * @param scenicSpots
     */
    public Rsp updateNewsinfo(ScenicSpots scenicSpots) {
        if(scenicSpots.getStatus() == 1){
            scenicSpots.setStatus(0);
        }else{
            scenicSpots.setStatus(1);
        }
        int i = scenicSpotsMapper.updateNewsinfo(scenicSpots.getId(), scenicSpots.getStatus());
        if(i == 1){
            return Rsp.success(null);
        }else {
            return Rsp.fail();
        }

    }

    //根据城市计算出周围的景点
    public List<ScenicSpots> getScenicSpotsByLatAndLng(MaxAndMinLatAndLng maxAndMinLatAndLng) {

        List<ScenicSpots> scenicSpotss = scenicSpotsMapper.getScenicSpotsByLatAndLng(maxAndMinLatAndLng);

        return scenicSpotss;
    }

    /**
     * 根据首页点击景点名查询景点表中的景点
     * @param scenicSpots
     * @return
     */
    public Rsp getscenicSpotsByName(ScenicSpots scenicSpots) {

        ScenicSpots sst = scenicSpotsMapper.getscenicSpotsByName(scenicSpots.getScenicSpotsName());

        if(StringUtils.isEmpty(sst)){
            return Rsp.fail().code(1);
        }else{
            return Rsp.success(sst);
        }



    }
}
